package com.focus.pay.wx.action.v3;

import cn.hutool.http.Method;
import com.focus.pay.wx.config.WxPayConfig;
import com.focus.pay.wx.constant.WxPayConstant;
import com.focus.pay.core.annation.PayOrderHandler;
import com.focus.pay.core.enums.ChannelTypeEnum;
import com.focus.pay.core.enums.Version;
import com.focus.pay.core.exception.PayException;
import com.focus.pay.core.pay.PayOrderInterface;
import com.focus.pay.core.utils.HttpClientUtils;
import com.focus.pay.wx.model.order.v3.WxPayModelUnified;
import com.focus.pay.wx.model.order.v3.WxPayModelUnifiedResponse;

/**
 * @author 赵参谋
 * @version $
 * =========================================================================
 * 变更履历：
 * -------------------------------------------------------------------------
 * 变更编号     变更时间    变更人   变更原因    变更内容
 * -------------------------------------------------------------------------
 * <p>
 * @description：
 * @date 2022/1/7 9:23
 **/
@PayOrderHandler(channelType = ChannelTypeEnum.WxType.H5,version = Version.VERSION_3)
public class WxPayOrderH5 extends WxPay implements PayOrderInterface<WxPayModelUnifiedResponse, WxPayModelUnified, WxPayConfig> {

    @Override
    public WxPayModelUnifiedResponse payOrder(WxPayModelUnified wxPayModelUnified, WxPayConfig wxPayConfig) throws PayException {
        DeployParams deployParams = super.instanceDeployParams(WxPayConstant.WxPayPathV3.PAY_ORDER_UNIFIED,new String[]{ChannelTypeEnum.WxType.H5});
        String res = HttpClientUtils.doPost(
                super.buildRequestPath(deployParams),
                super.buildBody(wxPayModelUnified,wxPayConfig),
                super.buildHead(Method.POST,wxPayModelUnified,wxPayConfig,deployParams));
        return this.handle(res,wxPayConfig);
    }

    /**
     * 返回数据处理
     * @throws Exception
     */
    private WxPayModelUnifiedResponse handle(String res, WxPayConfig wxPayConfig){
        WxPayModelUnifiedResponse wxPayModelUnifiedResponse = super.resolvingResult(res, WxPayModelUnifiedResponse.class);
        wxPayModelUnifiedResponse.setPageData(wxPayModelUnifiedResponse.getH5_url());
        return wxPayModelUnifiedResponse;
    }
}
